#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Shu Yang Wang
# Date: 2017/11/7
from django.conf import settings
from rbac import models

'''
初始化权限，获取当前用户权限并添加到session中
'''

def initial_permission(request, user):
    '''
    初始化权限
    :param request: 请求对象
    :param user: 当前用户对象 
    :return: 
    '''
    # 1.获得角色对应权限表数据，并去重.__isnull表示判断是否为空，无用
    permission_list = user.roles.filter(permissions__id__isnull=False).values(
        'permissions__id',  # 权限id
        'permissions__caption',  # 权限名
        'permissions__url',  # 权限url
        'permissions__code',  # 权限代码
        'permissions__menu_gp_id',  # 组内菜单ID， null表示为菜单
        'permissions__group__id',  # 权限组ID
        'permissions__group__menu_id',  # 权限组的菜单ID
        'permissions__group__menu__caption', # 权限组的菜单名称
        'permissions__group__menu__parent_id', # 父id
    ).distinct()

    # print(permission_list)
    '''
    2、格式化code,url转化为{{permissions__group_id:{codes:[], urls:[]}}}格式，
    菜单格式转化为[{'caption':'',url:'',menu_id:'',menu_caption:''}]格式，
    '''
    permission_dict = {}
    permission_menu_list = []
    for item in permission_list:
        code = item['permissions__code']
        group_id = item['permissions__group__id']
        url = item['permissions__url']

        if group_id in permission_dict:
            permission_dict[group_id]['codes'].append(code)
            permission_dict[group_id]['urls'].append(url)
        else:
            permission_dict[group_id] = {'codes': [code, ], 'urls': [url, ]}

        is_menu = item['permissions__menu_gp_id']
        if not is_menu:
            tmp = {
                'id': item['permissions__id'],
                'caption': item['permissions__caption'],
                'url': item['permissions__url'],
                'menu_gp_id': item['permissions__menu_gp_id'],
                'menu_id': item['permissions__group__menu_id'],
                'menu_title': item['permissions__group__menu__caption'],
                'menu_parent_id': item['permissions__group__menu__parent_id']
            }
            permission_menu_list.append(tmp)
    # 3、写入权限和菜单列表到session中
    request.session[settings.RBAC_PERMISSION_URL_SESSION_KEY] = permission_dict
    # 菜单权限列表
    menu_list = list(models.Menu.objects.all().values('id', 'caption', 'parent_id'))
    request.session[settings.RBAC_MENU_PERMISSION_SESSION_KEY] = {
        settings.RBAC_MENU_KEY: menu_list,
        settings.RBAC_MENU_PERMISSION_KEY: permission_menu_list
    }

    # print(permission_dict)
    # print(permission_menu_list)
    # print(menu_list)

    '''
    {1: {'codes': ['list', 'add'], 'urls': ['/app01/index', '/app01/add/']}, 
    2: {'codes': ['list', 'add'], 'urls': ['/app01/list2', '/app01/add2/']}, 
    3: {'codes': ['list'], 'urls': ['/app01/list3']}, 
    4: {'codes': ['list'], 'urls': ['/app01/list2']}}
    [
    {'id': 1, 'caption': '权限一', 'url': '/app01/index', 'menu_gp_id': None, 'menu_id': 1, 'menu_title': '菜单一', 'menu_parent_id': None}, 
    {'id': 2, 'caption': '权限一添加', 'url': '/app01/add/', 'menu_gp_id': None, 'menu_id': 1, 'menu_title': '菜单一', 'menu_parent_id': None}, 
    {'id': 3, 'caption': '权限二菜单', 'url': '/app01/list2', 'menu_gp_id': None, 'menu_id': 2, 'menu_title': '菜单二', 'menu_parent_id': None},
    {'id': 4, 'caption': '权限二添加', 'url': '/app01/add2/', 'menu_gp_id': None, 'menu_id': 2, 'menu_title': '菜单二', 'menu_parent_id': None}, 
    {'id': 5, 'caption': '权限组三菜单', 'url': '/app01/list3', 'menu_gp_id': None, 'menu_id': 3, 'menu_title': '菜单一一', 'menu_parent_id': 1}, 
    {'id': 6, 'caption': '权限四', 'url': '/app01/list2', 'menu_gp_id': None, 'menu_id': 4, 'menu_title': '菜单二二', 'menu_parent_id': 2}
    ]
    [
    {'id': 1, 'caption': '菜单一', 'parent_id': None}, 
    {'id': 2, 'caption': '菜单二', 'parent_id': None}, 
    {'id': 3, 'caption': '菜单一一', 'parent_id': 1}, 
    {'id': 4, 'caption': '菜单二二', 'parent_id': 2}
    ]
    '''